<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Realtime_item_model extends MY_Model {

	const TABLE = 'realtime_item';

	public function add($item_name, $item_key, $safe_value, $alarm_value, $item_type, $item_server, $alarm_content) {
		$data = array(
			'item_name' => $item_name,
			'item_key' => $item_key,
			'safe_value'  => $safe_value,
			'alarm_value' => $alarm_value,
			'item_type' => $item_type,
			'item_server' => $item_server,
			'alarm_content'  => $alarm_content,
		);
		if(!is_null($item_name)) {
			$this->db->insert(self::TABLE, $data);
		}
		return $this->db->insert_id();
	}

	public function delete($item_id) {
		$this->db->where('item_id', $item_id);
		$this->db->delete(self::TABLE);
	}

	public function update($item_id, $item_name, $item_key, $safe_value, $alarm_value, $item_type, $item_server, $alarm_content) {
		$data = array(
			'item_name' => $item_name,
			'item_key' => $item_key,
			'safe_value'  => $safe_value,
			'alarm_value' => $alarm_value,
			'item_type' => $item_type,
			'item_server' => $item_server,
			'alarm_content'  => $alarm_content,
		);
		$this->db->where('item_id', $item_id);
		$this->db->update(self::TABLE, $data); 
	}

	public function get_item_list() {
		$query = $this->db->query("select * from realtime_item order by item_id desc");
		return $query->result_array();
	}

	public function get_by_id($item_id) { 
		$query = $this->db->get_where(self::TABLE, array('item_id' => $item_id));
		return $query->row_array();
	}

	public function get_by_item_key($item_key) { 
		$query = $this->db->get_where(self::TABLE, array('item_key' => $item_key));
		return $query->row_array();
	}

	public function get_by_key_server($item_key,$item_server) {
		$query = $this->db->get_where(self::TABLE, array('item_key' => $item_key, 'item_server' => $item_server));
		return $query->row_array();
	}

	public function get_server_list() { 
		$query = $this->db->query("select distinct item_server from realtime_item");
		return $query->result_array();
	}

	public function get_all_item_name() { 
		$query = $this->db->query("select distinct item_name,item_id from realtime_item");
		return $query->result_array();
	}

	
}